//
//  YORequestBase.m
//  Youhuiju
//
//  Created by 余尚飞 on 2016/10/3.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "YORequestBase.h"
#import "YOSessionManager.h"

#define BaseUrl @"http://192.168.1.23:8080/lightSmart-rest/mobile/"

@implementation YORequestBase

-(instancetype)init
{
    self = [super init];
    if (self)
    {
//        self.url = @"";
        //用来设置请求需要的公共参数  例如：version devtoken session_id.....
        self.baseParams = [[NSMutableDictionary alloc]init];
    }
    return self;
}

//#pragma mark ----- 拼接完整的url
//-(NSString *)getCompleteUrl
//{
//    //获取一个baseUrl 拼接之后成一个完整的返回
//    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,_url];
//    return url;
//}

#pragma mark --  基本设置
//继承之后这两个方法需要覆写
//请求的方式
-(RequestMethod)getRequestMethod
{
    return RequestMethodPOST;
}

//是否包含图片上传
-(RequestType)getRequestType
{
    return RequestTypeOrdinary;
}

#pragma mark ------组合参数
-(NSDictionary *)combinationParams:(NSDictionary *)params
{
    [self.baseParams addEntriesFromDictionary:params];
    return self.baseParams;
}

#pragma mark --- 发起请求
+(NSURLSessionDataTask *)requestDataWithAPIName:(NSString *)apiName Params:(NSDictionary *)params success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
    YORequestBase *requestBase = [[self alloc]init];
    if (requestBase)
    {
        NSDictionary *allParams = [requestBase combinationParams:params];
        if ([requestBase getRequestType] == RequestTypeOrdinary) {
            return [requestBase RequestNoImageAPIName:apiName Params:allParams success:success failure:failure];
        }else{
            return [requestBase RequestImageAPIName:apiName Params:allParams success:success failure:failure];
        }
    }
    return nil;
}

#pragma mark - Request no Image


-(NSURLSessionDataTask *)RequestNoImageAPIName:(NSString *)apiName
                                        Params:(NSDictionary *)params
                               success:(void(^)(NSURLSessionDataTask *task,id responeObject))success
                               failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{

    
    //拼接接口
    NSString *url = [NSString stringWithFormat:@"%@%@.do",BaseUrl,apiName];
    
    DLog(@"\n=====================================================\n\n\nurl:%@\nparams:%@\n\n\n====================================================",url,params);
    
    
    
    
    YOSessionManager *manager = [YOSessionManager shareInstance];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    if ([self getRequestMethod] == RequestMethodGET)
    {
         NSString *url = [NSString stringWithFormat:@"%@%@.do",BaseUrl,apiName];
        NSURLSessionDataTask *task = [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(task,responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            //在这里可以对请求出错做统一处理
            failure(task,error);
        }];
        return task;
    }else{
    
        NSURLSessionDataTask *task = [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(task,responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            //在这里可以对请求出错做统一处理
            failure(task,error);
        }];
        return task;
    }
}

#pragma mark - Request  Image
-(NSURLSessionDataTask *)RequestImageAPIName:(NSString *)apiName
                                      Params:(NSDictionary *)params
                               success:(void(^)(NSURLSessionDataTask *task,id responeObject))success
                                    failure:(void(^)(NSURLSessionDataTask *task,NSError *error))failure{
    //拼接接口
    NSString *url = [NSString stringWithFormat:@"%@%@.do",BaseUrl,apiName];
    
    DLog(@"\n=====================================================\n\n\nurl:%@\n\nparams:%@\n\n\n====================================================",url,params);
    
    
    
    YOSessionManager *manager = [YOSessionManager shareInstance];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSURLSessionDataTask *task = [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //构造body,告诉af是什么类型文件
        for (NSString *key in params)
        {
            id value = params[key];
            if ([value isKindOfClass:[NSData class]])
            {
                [formData appendPartWithFileData:value name:key fileName:[NSString stringWithFormat:@"%@.jpg",key] mimeType:@"image/jpeg"];
            }
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //在这里可以对请求出错做统一处理
        failure(task,error);
    }];
    
    return task;
    
    
    
    
}















@end
